//安装express依赖 并用变量保存起来方便调用里面的方法
const { color } = require("echarts");
var express = require("express");
var app = express();
//配置地址与端口号
var hostName = '127.0.0.1';
var port = 9000;

var Mock = require("mockjs")
const userArr = []
const goodsArr = []
const borrowArr = []
const shoppingArr = []

function generateUsername(length = 8) {
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    let result = '';
    const charactersLength = characters.length;
    for (let i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}

for (let i = 0; i < 50; i++) {
    userArr.push({
        index: i + 1,
        userid: Mock.mock(/[1-9]\d{9}/),
        username: generateUsername(),
        password: '123456',
        name: Mock.mock('@cname'),
        sex: Mock.Random.integer('0', '1').toString(),
        age: Mock.Random.integer(18, 30),
        email: Mock.mock('@email'),
        phone: Mock.mock(/1\d{10}/),
        role_id: Mock.Random.integer('1', '2').toString(),
    })
}

for (var i = 0; i < 50; i++) {
    goodsArr.push({
        index: i + 1,//序号
        goods_id: Mock.mock(/[1-9]\d{5}/),//id
        goods_name: Mock.mock('@ctitle(4,8)'),//商品名称
        brand: Mock.mock('@cname'),//品牌
        category: Mock.Random.integer('1', '8').toString(),//类别
        seller: Mock.mock('@cname'),//销售商
        manufacturer: Mock.mock('@cname'),//生产商
        release_date: Mock.mock(/20\d{2}-[01]\d-[0-3]\d/),//发售日期
        price: Mock.mock('@integer(10,100)'),//价格
        num: Mock.mock('@integer(0,1000)'),//库存
        imgurl: (i + 1) + '.jpg',//图片
        description: Mock.mock('@cparagraph'),//描述
    })
}

for (var i = 0; i < 50; i++) {
    borrowArr.push({
        index: i + 1,//序号
        borrow_id: Mock.mock(/[1-9]\d{5}/),//id
        book_id: Mock.mock(/[1-9]\d{5}/),//书id
        user_id: Mock.mock(/[1-9]\d{8}/),//用户id
        borrow_date: Mock.mock(/20\d{2}-[01]\d-[0-3]\d/),//借书日期
        return_date: Mock.mock(/20\d{2}-[01]\d-[0-3]\d/),//还书日期
        status: Mock.Random.integer('0', '2').toString(),//状态
    })
}

for (var i = 0; i < 4; i++) {
    shoppingArr.push({
        goods_name: Mock.mock('@ctitle(4,8)'),//商品名称
        goods_url: (i + 1) + '.jpg',//id
        num: 1,//数量
        price: Mock.mock('@integer(10,100)'),//价格

    })
}

//消息
const activities = []
for (var i = 0; i < 4; i++) {
    activities.push({
        icon: Mock.Random.integer('0', '1').toString(),
        timestamp: Mock.mock(/20\d{2}-[01]\d-[0-3]\d/),
        content: Mock.mock('@cparagraph'),
        color: '#91cc75',
    })
}
//对所有请求开启CORS  解决跨域问题
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
//用户登录请求
app.post("/user/login", function (req, res) {
    var data = '';
    req.on('data', function (chunk) {
        data += chunk;
    })
    req.on('end', function () {
        var _data = JSON.parse(data)
        var result = {
            code: 200,
            msg: "请求成功",
            data: false,
        };
        if (_data.username == 'admin' && _data.password == '123456') {
            result.data = true;
        }
        else {
            result.data = false;
        }
        res.send(result);
    })
})
app.post("/login", function (req, res) {
    res.send("1111")
})
//用户修改密码请求
app.post("/user/changePassword", function (req, res) {
    var data = '';
    req.on('data', function (chunk) {
        data += chunk;
    })
    req.on('end', function () {
        var _data = JSON.parse(data)
        var result = {
            code: 200,
            msg: "请求成功",
            data: false,
        };
        if (_data.oldpassword == '123456') {
            result.data = true;
        }
        else {
            result.data = false;
        }
        res.send(result);
    })
})
//获取用户信息
app.get("/user/getMessage", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功",
        data: activities
    }
    res.send(result);
})
//修改个人信息
app.post("/user/changeuser", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功",
        data: false,
    };
    res.send(result);
})

//获取用户列表请求
app.get("/user/getList", function (req, res) {
    // console.log(userArr)
    var result = {
        code: 200,
        msg: "请求成功",
        data: userArr
    }
    res.send(result);
})
//删除用户请求
app.post("/user/delete", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功",
    }
    res.send(result);
})
//修改用户信息请求
app.post("/user/update", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功",
    }
    res.send(result);
})
//添加用户请求
app.post("/user/add", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功",
    }
    res.send(result);
})
//更新用户请求
app.post("/user/search", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功",
    }
    res.send(result);
})
//获取商品列表请求
app.get("/goods/getlist", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功",
        data: goodsArr
    }
    res.send(result);
})
//删除商品请求
app.post("/goods/delete", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功",
    }
    res.send(result);
})
//修改商品信息请求
app.post("/goods/edit", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功"
    }
    res.send(result)
})
//添加商品请求
app.post("/goods/add", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功"
    }
    res.send(result)
})
//更新商品请求
app.post("/goods/search", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功"
    }
    res.send(result)
})


//获取借书列表请求
app.get("/borrow/getlist", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功",
        data: borrowArr
    }
    res.send(result);
})
//删除借书请求
app.post("/borrow/delete", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功",
    }
    res.send(result);
})
//添加借阅请求
app.post("/borrow/add", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功",
    }
    res.send(result);
})
//修改借阅请求
app.post("/borrow/update", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功",
    }
    res.send(result);
})
app.post("/borrow/search", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功"
    }
    res.send(result)
})
app.post("/user/feedback", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功"
    }
    res.send(result)
})
app.get("/goods/shopping", function (req, res) {
    var result = {
        code: 200,
        msg: "请求成功",
        data: shoppingArr
    }
    res.send(result);
})



app.listen(port, hostName, function () {
    console.log(`启动成功,服务器运行在http://${hostName}:${port}`);
});